<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Fault List</title>

    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" rel="stylesheet">

    <link href="../../static/layui/css/layui.css" th:href="@{/webjars/layui/css/layui.css}" rel="stylesheet">

    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js" th:src="@{/webjars/jquery/jquery.min.js}"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/distpicker/2.0.5/distpicker.min.js"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/store.js/1.3.20/store.min.js" th:src="@{/webjars/store.js/store.min.js}"></script>

    <script type="text/javascript" src="../../static/layui/layui.all.js" th:src="@{/webjars/layui/layui.all.js}"></script>
    <script type="text/javascript" src="../../static/layui/layui.js" th:src="@{/webjars/layui/layui.js}"></script>
    <script type="text/javascript" src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    <!-- 头部导航栏 -->
    <div id="header-navigation-div-id"></div>

    <!-- 侧边导航栏 -->
    <div id="side-navigation-div-id"></div>

    <!-- 内容主体区域 -->
    <div class="layui-body" style="left: 300px;">

        <!--    搜索表单    -->
        <div style="padding: 15px;">
            <div class="layui-container" style="
                border: 1px solid #1E9FFF;
                margin: 10px auto;
                padding: 20px;
                background-color: #e2e2e2;
                border-radius: 10px;
                  ">
                <h3 style="text-align: center">查询条件</h3>
                <form id="state-search-form" class="form-horizontal" role="form" style="width: 95%; margin-top: 30px;">
                    <div class="form-group">
                        <label for="fault-type" class="col-sm-1 control-label">故障类型</label>
                        <div class="col-sm-5">
                            <select id="fault-type" class="form-control">
                                <option value="">---- 选择故障类型 ----</option>
                                <option value="REFRIGERATION_SYSTEM_BLOCKAGE">制冷系统堵</option>
                                <option value="REFRIGERATION_SYSTEM_LEAKAGE">制冷系统漏</option>
                                <option value="FOUR_WAY_VALUE_FAULT">四通阀故障</option>
                                <option value="ONE_WAY_VALUE_FAULT">单向阀故障</option>
                            </select>
                        </div>
                        <label for="fault-state" class="col-sm-1 control-label">故障状态</label>
                        <div class="col-sm-5">
                            <select id="fault-state" class="form-control">
                                <option value="">---- 选择故障类型 ----</option>
                                <option value="NEW">新建</option>
                                <option value="ALLOCATED">已分配</option>
                                <option value="REPAIRING">正在维修</option>
                                <option value="REPAIRED">已维修</option>
                                <option value="REVISITED">已回访</option>
                                <option value="END">维修结束</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10" style="width: 40%; margin: 0 auto">
                            <button class="btn btn-success" id="search-btn" style="margin: 20px 0 0 65%; width: 100px;">查询</button>
                        </div>
                        <div class="col-sm-offset-2 col-sm-10" style="width: 40%; margin: 0 auto">
                            <button class="btn btn-danger" id="reset-btn" style="margin: 20px 0 0 65%; width: 100px;">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!--  分割线  -->
        <hr class="layui-bg-blue" style="margin-bottom: 30px;">

        <!--    数据展示表格    -->
        <div style="margin: 0 10px; padding: 0 10px; border: 1px solid #1E9FFF; border-radius: 5px;">
            <table id="dataTableId" lay-filter="dataTableId"></table>
        </div>

    </div>

    <!-- 页脚 -->
    <div id="footer-div-id"></div>
</div>

<script type="text/javascript">

    window.onload = function (ev) {

        // 页面初始化
        pageAfterOnloadInit();

        // 设置“查询”按钮事件
        document.getElementById('search-btn').onclick = stateSearchFormSubmitEvent;

        // 设置“重置”按钮事件
        document.getElementById('reset-btn').onclick = stateSearchFormResetEvent;


        // 退出事件
        document.getElementById('logout-btn-id').onclick = logoutEvent;
    }

</script>

<script type="text/html" id="tableBarOrdinary">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="tableBarCustomRepair">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
</script>

<script type="text/javascript">

    //////////////// function defined ///////////////////////////////

    // 页面初始化，可以进入这个页面的身份有：ORDINARY、CUSTOM_SERVICE、REPAIR
    function pageAfterOnloadInit() {

        initHeaderNavigationDiv();

        var loginUser = getValidValueFromStore(LOGIN_USER_KEY);
        if (loginUser.role === 'ORDINARY') {
            initSideNavigationDivOrdinaryFault();
        } else if(loginUser.role === 'CUSTOM_SERVICE') {
            initSideNavigationDivCustomFault();
        } else {
            initSideNavigationDivRepairFault();
        }
        initFooterDiv();
        // layui 初始化
        layuiInit();

        // layui 数据表格初始化
        if (loginUser.role === 'ORDINARY') {
            layuiTableInitOrdinary();
        } else {
            layuiTableInitCustomOrRepair();
        }

    }

    // customOrRepair layui table 初始化
    function layuiTableInitCustomOrRepair() {
        layui.use('table', function () {
            var table = layui.table;

            // 表格渲染
            table.render({
                elem: '#dataTableId',
                id: 'dataTableId',
                url: FAULT_BASE_MAPPING_V1 + "/page",  // 默认数据查询数据接口
                loading: true,  // 显示加载条
                page: true,     // 开启分页
                even: true,     // 设置隔行背景
                response: {
                    statusCode: 200     //规定成功的状态码，默认：0
                },
                parseData: function (result) { //result 即为原始返回的数据
                    var obj = JSON.parse(result);
                    var code = obj.code;
                    var message = obj.message;
                    var data = obj.data;
                    if (code !== 200) {
                        message = "请您先登录！";
                    }
                    return {
                        "code": code,               //解析接口状态
                        "msg": message,             //解析提示文本
                        "count": data.count,        //解析数据长度
                        "data": data.data           //解析数据列表
                    };
                },
                cols: [
                    [   //表头
                        {field: 'id', hide: true},
                        {field: 'equipmentId', title: '设备编号', sort: true, align: 'center', width: 169},
                        {field: 'type', title: '故障类型', sort: true, align: 'center', width: 169},
                        {field: 'state', title: '故障状态', sort: true, align: 'center', width: 169},
                        {field: 'gmtModified', title: '刷新时间', sort: true, align: 'center', width: 169},
                        {field: 'description', title: '故障描述', sort: true, align: 'center'},
                        {title: '操作', width: 129, align: 'center', toolbar: '#tableBarCustomRepair'}
                    ]
                ]
            });

            //监听行工具事件，行末按钮事件
            table.on('tool(dataTableId)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data;         //获得当前行数据
                var layEvent = obj.event;    //获得 lay-event 对应的值
                if(layEvent === 'detail'){

                    // 打开 update-info 页面
                    var url = FAULT_BASE_MAPPING_V1 + "/page/update?faultId=" + data.id;
                    window.open(url);

                }
            });
        });
    }

    // odinary layui 数据表格初始化
    function layuiTableInitOrdinary() {
        layui.use('table', function () {
            var table = layui.table;

            // 表格渲染
            table.render({
                elem: '#dataTableId',
                id: 'dataTableId',
                url: FAULT_BASE_MAPPING_V1 + "/page",  // 默认数据查询数据接口
                loading: true,  // 显示加载条
                page: true,     // 开启分页
                even: true,     // 设置隔行背景
                response: {
                    statusCode: 200     //规定成功的状态码，默认：0
                },
                parseData: function (result) { //result 即为原始返回的数据
                    var obj = JSON.parse(result);
                    var code = obj.code;
                    var message = obj.message;
                    var data = obj.data;
                    if (code !== 200) {
                        message = "请您先登录！";
                    }
                    return {
                        "code": code,               //解析接口状态
                        "msg": message,             //解析提示文本
                        "count": data.count,        //解析数据长度
                        "data": data.data           //解析数据列表
                    };
                },
                cols: [
                    [   //表头
                        {field: 'id', hide: true},
                        {field: 'equipmentId', title: '设备编号', sort: true, align: 'center', width: 169},
                        {field: 'type', title: '故障类型', sort: true, align: 'center', width: 169},
                        {field: 'state', title: '故障状态', sort: true, align: 'center', width: 169},
                        {field: 'gmtModified', title: '刷新时间', sort: true, align: 'center', width: 169},
                        {field: 'description', title: '故障描述', sort: true, align: 'center'},
                        {fixed: 'right', title: '操作', width: 129, align: 'center', toolbar: '#tableBarOrdinary'}
                    ]
                ]
            });

            //监听行工具事件，行末按钮事件
            table.on('tool(dataTableId)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data;         //获得当前行数据
                var layEvent = obj.event;    //获得 lay-event 对应的值
                if(layEvent === 'detail'){

                    // 打开 update-info 页面
                    var url = FAULT_BASE_MAPPING_V1 + "/page/update?faultId=" + data.id;
                    window.open(url);

                } else if(layEvent === 'del'){

                    if (data.state !== '新建') {
                        kingLayerMsg('禁止删除非新建状态的故障！', airConditionerDefaultLayerMsgOptions);
                        return false;
                    }

                    layer.confirm('确认删除?', {icon: 2, title: '提示'}, function(index){
                        layer.close(index);

                        //向服务端发送删除指令
                        var url = FAULT_BASE_MAPPING_V1 + "/" + data.id + "/delete";
                        $.ajax({
                            url: url,
                            type: "post",
                            success: function (result, status, xhr) {
                                var resultObj = JSON.parse(result);
                                var code = resultObj.code;
                                var message = resultObj.message;
                                if (code !== 200) {
                                    kingLayerMsg(message, airConditionerDefaultLayerMsgOptions);
                                } else {
                                    kingLayerMsg("删除成功！", airConditionerDefaultLayerMsgOptions);
                                    var data = getTableDataFromServer();
                                    // 表格重载
                                    layui.use('table', function () {
                                        var table = layui.table;
                                        table.reload('dataTableId', {
                                            data: data,
                                            url: ''  // 数据接口置为空
                                        })
                                    });
                                }
                            }
                        });
                    });
                }
            });
        });
    }

    function stateSearchFormSubmitEvent(ev) {

            // 发送查询设备信息请求
            $.ajax({
                url: FAULT_BASE_MAPPING_V1 + "/search",  // 带查询条件的查询
                type: "get",
                data: {
                    'type': $("#fault-type").val().trim(),
                    'state': $("#fault-state").val().trim()
                },
                success: function (result, status, xhr) {
                    var obj = JSON.parse(result);
                    var code = obj.code;
                    var message = obj.message;
                    var data = obj.data;
                    if (code !== 200) {
                        kingLayerMsg(message, airConditionerDefaultLayerMsgOptions);
                    } else {
                        kingLayerMsg("查询成功！", airConditionerDefaultLayerMsgOptions);
                        // 表格重载
                        layui.use('table', function () {
                            var table = layui.table;
                            table.reload('dataTableId', {
                                data: data,
                                url: ''  // 数据接口置为空
                            })
                        });
                    }
                }
            });
        return false;
    }

    function stateSearchFormResetEvent(ev) {
        var form = document.getElementById('state-search-form');

        var selects = form.getElementsByTagName('select');
        for (i = 0; i < selects.length; i++) {
            selects[i].options[0].selected = true;
        }

        var data = getTableDataFromServer();

        // 表格重载
        layui.use('table', function () {
            var table = layui.table;
            table.reload('dataTableId', {
                data: data,
                url: ''  // 数据接口置为空
            })
        });
        return false;
    }

    // 查询数据
    function getTableDataFromServer() {
        var outResult = {};
        $.ajax({
            url: FAULT_BASE_MAPPING_V1,  // 按用户查询所有设备信息
            type: "get",
            async: false,
            success: function (result, status, xhr) {
                var obj = JSON.parse(result);
                var code = obj.code;
                var message = obj.message;
                var data = obj.data;
                if (code !== 200) {
                    kingLayerMsg(message, airConditionerDefaultLayerMsgOptions);
                } else {
                   outResult = data;
                }
            }
        });
        return outResult;
    }

</script>
</body>
</html>